package com.kzd.sdk.ui.fragment;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnItemChildClickListener;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.entity.KZD_GameService;
import com.kzd.sdk.ui.adapter.KZD_GameServiceAdapter;
import com.kzd.sdk.utils.AppUtils;

public class KZD_HomeServiceFragment extends OwnerFragment {

    private RecyclerView rvHomeServiceSoonBody, rvHomeServiceStopBody;

    private KZD_GameServiceAdapter mGameServiceSoonAdapter, mGameServiceStopAdapter;

    private final KZD_HttpCallback<KZD_GameService> httpListenerGameService = new KZD_HttpCallback<KZD_GameService>() {
        @Override
        public void onSuccess(KZD_GameService data) {
            mGameServiceSoonAdapter.setNewInstance(data.getSoonList());
            mGameServiceStopAdapter.setNewInstance(data.getStopList());
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    private final KZD_HttpCallback<String> httpListenerRemindService = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            KZD_ApiManager.httpGameService(httpListenerGameService);
            toast(data);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_fragment_home_service";
    }

    @Override
    public void initView(View view) {
        rvHomeServiceSoonBody = view.findViewById(AppUtils.getId(mContext, "rvHomeServiceSoonBody"));
        rvHomeServiceStopBody = view.findViewById(AppUtils.getId(mContext, "rvHomeServiceStopBody"));

        mGameServiceSoonAdapter = new KZD_GameServiceAdapter(mContext,1);
        rvHomeServiceSoonBody.setLayoutManager(new LinearLayoutManager(mContext));
        rvHomeServiceSoonBody.setAdapter(mGameServiceSoonAdapter);


        mGameServiceStopAdapter = new KZD_GameServiceAdapter(mContext,2);
        rvHomeServiceStopBody.setLayoutManager(new LinearLayoutManager(mContext));
        rvHomeServiceStopBody.setAdapter(mGameServiceStopAdapter);

        mGameServiceSoonAdapter.setOnItemChildClickListener(new KZD_OnItemChildClickListener() {
            @Override
            public void onClick(View v, int position) {
                String id = mGameServiceSoonAdapter.get(position)
                        .getId();
                if (TextUtils.isEmpty(id)) {
                    return;
                }
                KZD_ApiManager.httpRemindService(id,httpListenerRemindService);
            }
        });
    }

    @Override
    public void initData() {
        KZD_ApiManager.httpGameService(httpListenerGameService);
    }
}
